#!/bin/bash

sudo mount -rw -o remount /
sudo git pull

baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
echo "full path to currently executed script is : ${baseDirForScriptSelf}"

#serail_port=$1

#if [ ! $serail_port ]; then
#	echo serail_port was null.
#	exit 0
#fi
serailupfifo="/tmp/serailupfifo"
mqttdownfifo="/tmp/mqttdownfifo"
httpdownloadfifo="/tmp/httpdownloadfifo"
log_path="/home/pi/creality/log"
print_file_path="/home/pi/creality/printerFiles"
log_prt="/tmp/debug/prt_com"
log_clould="/tmp/debug/led"



printer_detect="/etc/udev/rules.d/98-3d-printer.rules"
if [ ! -f "$printer_detect" ]; then
        echo "$printer_detect" was not exist!
        # uart hotplug
        sudo echo "ACTION==\"add\", ATTRS{product}==\"USB Serial\", SUBSYSTEMS==\"usb\", RUN+=\"$baseDirForScriptSelf/usbdrive.sh &\"
        ACTION==\"remove\", ATTRS{product}==\"USB Serial\",  SUBSYSTEMS==\"usb\", RUN+=\"$baseDirForScriptSelf/usbdrive.sh &\"" > $baseDirForScriptSelf/98-3d-printer.rules
        sudo mv "$baseDirForScriptSelf"/98-3d-printer.rules /etc/udev/rules.d/
#       exit 0
fi


printer_com_project="$baseDirForScriptSelf/prtSerial"
if [ ! -f "$printer_com_project" ]; then
	echo "$printer_com_project" was not exist!
	exit 0
fi
RaspberryPiCreality="$baseDirForScriptSelf/RaspberryPiCreality"
if [ ! -f "$RaspberryPiCreality" ]; then
	echo "$RaspberryPiCreality" was not exist!
	exit 0
fi
DownFile="$baseDirForScriptSelf/DownFile"
if [ ! -f "$DownFile" ]; then
	echo "$DownFile" was not exist!
	exit 0
fi




if [ ! -p "$serailupfifo" ]; then
	echo "$serailupfifo" was not exist!
	mkfifo "$serailupfifo"
fi
echo "$serailupfifo" was exist!
if [ ! -p "$mqttdownfifo" ]; then
	echo "$mqttdownfifo" was not exist!
	mkfifo "$mqttdownfifo"
fi
echo "$mqttdownfifo" was exist!
if [ ! -p "$httpdownloadfifo" ]; then
	echo "$httpdownloadfifo" was not exist!
	mkfifo "$httpdownloadfifo"
fi
echo "$httpdownloadfifo" was exist!

if [ ! -d "$log_path" ]; then
	echo "$log_path" was not exist!
	mkdir -p "$log_path"
fi
echo "$log_path" was exist!
if [ ! -d "$print_file_path" ]; then
	echo "$print_file_path" was not exist!
	mkdir -p "$print_file_path"
fi
echo "$print_file_path" was exist!
if [ ! -d "$log_prt" ]; then
	echo "$log_prt" was not exist!
	mkdir -p "$log_prt"
fi

echo "$log_prt" was exist!
if [ ! -d "$log_clould" ]; then
	echo "$log_clould" was not exist!
	mkdir -p "$log_clould"
fi
echo "$log_clould" not exist!

#connect printer check
printer_value="/dev/ttyUSB0"
baudrate_value=115200
printer_couter=0
printer=$(ls -rtl /dev/ | grep "ttyUSB" | grep -v grep | awk '{print $10}')
#echo "$printer"
baudrate_arr=(115200 250000)

if [ -n "$printer" ]; then
        #echo Printer  was not connected, please connect to creality 3D printer from Serial!
        #echo And then restart printer services [ creality_printer.service ]!
        #exit 0
	for baudrate in ${baudrate_arr[@]}
	do
		echo baudrate was $baudrate
		sudo "$GenerateIdentifier" -b $baudrate -p /dev/"$printer" > /dev/null
		return_val=$?
		if [[ $return_val =~ 0 ]]
		then
		#	exit
			echo  The /dev/"$printer" was not printer Serial.
		else
			#$printer_cout=$((${printer_cout} + 1))
			printer_couter=1
			printer_value="$printer"
			baudrate_value="$baudrate"

			echo "$printer":"$baudrate" > printer_serial.txt
			break

		fi
	done
fi

if [[ $printer_couter -eq 0 ]]; then
	echo "3D Printer was not connected, please connected!"
	exit
fi

#sudo  "$printer_com_project" -p "$serail_port" > /dev/null &
sudo  "$printer_com_project" -a /dev/"$printer_value" -b "$baudrate_value"  &
sleep 1
sudo  "$DownFile" &
cd "$baseDirForScriptSelf" && sudo ./RaspberryPiCreality  > /dev/null

